var gl_material_vue_vm_list_html = $('#material_vue_vm_list').html();
var pageParam = null;

$(function() {
	$('#vue_vm_list div').hide();
	
	var pageParamStr = $.url().param('pageParam');
	pageParam = JSON.parse(pageParamStr);
	showParamInfo(pageParam);
});

function showParamInfo(pageParam) {
	if($('#vue_vm_list').length==0) {return;}
	$('#vue_vm_list div').hide();
	$('#vue_vm_list').html(gl_vue_vm_list_html);
	
	new Vue({
		el: '#vue_vm_list',
		data: {
			todos: [pageParam]
		},
		methods: {
			onDataChange:function(todo) {
				onDataChange(todo);
			}
		}
	});
	
	$('#vue_vm_list div').show();
}

function onDataChange(obj) {
	obj.leaseStartTime = getStandardDay(obj.startYear, obj.startMonth, obj.startDay);
	obj.leaseEndTime = getStandardDay(obj.endYear, obj.endMonth, obj.endDay);
	//obj.leaseTime = getStandardDay(obj.leaseYear, obj.leaseMonth, obj.leaseDay);
	obj.leaseTime = obj.leaseStartTime;
	obj.leaseDayNum = DateDiff(obj.leaseEndTime, obj.leaseStartTime);
	obj.totalRent = obj.rent * obj.leaseDayNum;
	obj.totalMoney = parseFloat(obj.officialRent) + parseFloat(obj.officialPledge);
	
	$('#leaseDayNum').text(obj.leaseDayNum+'天');
	$('#totalRent').text(obj.totalRent+'元');
	$('#pledge').text(obj.pledge+'元');
	$('#totalMoney').text(obj.totalMoney+'元');
}

function saveGoNextPage() {
	onDataChange(pageParam);
	
	pageParam.leaseType = 1;
	pageParam.leaseStatus = 1;
	pageParam.totalFee = pageParam.totalMoney;
	pageParam.payWay = parseInt($('input:radio[name="payWay"]:checked').val());
	if((pageParam.payWay==3 || pageParam.payWay==4) && pageParam.officialRent!=0) {
		alert('五保免租金，请在租金实收中输入0');
		return;
	}
	if((pageParam.payWay==4) && pageParam.officialPledge!=0) {
		alert('五保免押金，请在押金实收中输入0');
		return;
	}
	if(isEmpty(pageParam.totalMoney)) {
		alert('金额不正确');
		return;
	}
	
	var loginedUser = getLoginedUser();
	if(loginedUser) {
		pageParam.staffId = loginedUser.staffId ;
		pageParam.staffName = loginedUser.staffName;
		pageParam.subjectRoomId = loginedUser.subjectRoomId;
		pageParam.subjectRoomName = loginedUser.subjectRoomName;
	}
	
	save_basic(path+'/lease/save', {lease:pageParam}, function(response) {
		alert('租赁成功');
		pageParam.createTime = response.data[0].createTime;
		var url = '5-1-1ynrent-3.html?pageParam=' + encodeURIComponent(JSON.stringify(pageParam));
		window.open(url);
	});
}

function deleteBatch() {
	deleteBatch_basic(path+'/lease/deleteBatch');
}

function findElder(onSuccess) {
	var searchVal = pageParam.elderName;
	
	find_basic(path+'/elder/find', searchVal, function(response) {
		if(response.data.length>0) {
			pageParam.elderId = response.data[0].elderId;
			
			if(onSuccess) {
				onSuccess();
			}
		}
	});
}